package http_servicer

import (
	//	"encoding/json"
	"fmt"
	"ifms/db_servicer"
	"net/http"
	"strconv"
)

func fetchChildAddrHandler(response http.ResponseWriter, request *http.Request) {
	// logger = log.New(os.Stdout, ">>>>>>> Fetch Child Address:", log.LstdFlags|log.Lshortfile)

	v_parent_id := request.FormValue("parent_id")

	///	session_id , user_id := getAccountName(request)

	var parent_id int
	var msg string = "不是合法的数字:"

	var err error

	if parent_id, err = strconv.Atoi(v_parent_id); err != nil && v_parent_id != "" {
		msg = msg + " parent_id:" + v_parent_id
	}

	if msg > "不是合法的数字:" {
		logger.Println("\nError:", err)
		logger.Println("\nMsg:", msg)
		fmt.Fprintf(response, `{"flag":-1,"message":"%s" }`, msg)
	} else {

		dbmsg := db_servicer.Fetch_Child_Addr(parent_id)

		//	logger.Println("dbmsg:", dbmsg)
		logger.Println("\n>>>Return to App Client:", dbmsg.Data)
		fmt.Fprint(response, dbmsg.Data)
	}
}

func fetchCountriesHandler(response http.ResponseWriter, request *http.Request) {
	// logger = log.New(os.Stdout, ">>>>>>> Fetch Countries List :", log.LstdFlags|log.Lshortfile)

	continentCode := request.FormValue("continent_code")

	///	session_id , user_id := getAccountName(request)

	dbmsg := db_servicer.FetchContries(continentCode)

	//	logger.Println("dbmsg:", dbmsg)
	logger.Println("\n>>>Return to App Client:", dbmsg.Data)
	fmt.Fprint(response, dbmsg.Data)
}

func fetchContinentsHandler(response http.ResponseWriter, request *http.Request) {
	// logger = log.New(os.Stdout, ">>>>>>> Fetch Child Address:", log.LstdFlags|log.Lshortfile)

	///	session_id , user_id := getAccountName(request)

	dbmsg := db_servicer.Fetch_Continents_List()

	//	logger.Println("dbmsg:", dbmsg)
	logger.Println("\n>>>Return to App Client:", dbmsg.Data)
	fmt.Fprint(response, dbmsg.Data)
}

func modifyUserAlertNumHandler(response http.ResponseWriter, request *http.Request) {
	// logger = log.New(os.Stdout, ">>>>>>> Modify User Alert Number:", log.LstdFlags|log.Lshortfile)

	v_session_id, v_user_id := getAccountName(request)

	//	logger.Println(">>>session_id:" , session_id,", user_id :", user_id  )

	if v_session_id == "" || v_user_id == 0 {
		reloginPage(response, request)
		return
	}

	v_old_alert_num := request.FormValue("old_alert_num")
	v_new_alert_num := request.FormValue("new_alert_num")

	logger.Println(" OLD and NEW num:", v_old_alert_num, ",", v_new_alert_num)

	var old_alert_num, new_alert_num int64

	var msg string = "不是合法的数字:"
	var err error

	if old_alert_num, err = strconv.ParseInt(v_old_alert_num, 10, 64); err != nil && v_old_alert_num != "" {
		msg = msg + " old_alert_num:" + v_old_alert_num
	}
	if new_alert_num, err = strconv.ParseInt(v_new_alert_num, 10, 64); err != nil && v_new_alert_num != "" {
		msg = msg + " new_alert_num:" + v_new_alert_num
	}

	logger.Println("\nMsg:", msg)

	if msg > "不是合法的数字:" {
		logger.Println("\nError:", err)
		logger.Println("\nMsg:", msg)
		fmt.Fprintf(response, `{"flag":-1,"message":"%s" }`, msg)
	} else {

		dbmsg := db_servicer.Modify_User_Alert_Num(v_user_id, old_alert_num, new_alert_num)

		//	logger.Println("dbmsg:", dbmsg)
		sendAlertNum2Device(v_user_id)

		logger.Println("\n>>>Return to App Client:", dbmsg.Data)
		fmt.Fprint(response, dbmsg.Data)
	}
}

func modiUserAddrHandler(response http.ResponseWriter, request *http.Request) {
	// logger = log.New(os.Stdout, ">>>>>>> Modify User Address:", log.LstdFlags|log.Lshortfile)

	v_session_id, v_user_id := getAccountName(request)

	//	logger.Println(">>>session_id:" , session_id,", user_id :", user_id  )

	if v_session_id == "" || v_user_id == 0 {
		reloginPage(response, request)
		return
	}

	street_info := request.FormValue("street_info")
	v_addr_id := request.FormValue("addr_id")
	v_country_id := request.FormValue("country_id")

	var addr_id, country_id int

	var msg string = "不是合法的数字:"
	var err error

	if addr_id, err = strconv.Atoi(v_addr_id); err != nil && v_addr_id != "" {
		msg = msg + " addr_id  :" + v_addr_id
	}
	if country_id, err = strconv.Atoi(v_country_id); err != nil && v_country_id != "" {
		msg = msg + " country_id  :" + v_country_id
	}

	if msg > "不是合法的数字:" {
		logger.Println("\nError:", err)
		logger.Println("\nMsg:", msg)
		fmt.Fprintf(response, `{"flag":-1,"message":"%s" }`, msg)
	} else {

		dbmsg := db_servicer.Modify_User_Addr(v_user_id, addr_id, country_id, street_info)

		//	logger.Println("dbmsg:", dbmsg)
		logger.Println("\n>>>Return to App Client:", dbmsg.Data)
		fmt.Fprint(response, dbmsg.Data)
	}
}

func fetchUserInfoHandler(response http.ResponseWriter, request *http.Request) {
	// logger = log.New(os.Stdout, ">>>>>>> Fetch Child Address:", log.LstdFlags|log.Lshortfile)

	v_session_id, v_user_id := getAccountName(request)

	logger.Println(" SessionID:", v_session_id, ", User ID:", v_user_id)

	dbmsg := db_servicer.Fetch_User_Info(v_user_id)

	//	logger.Println("dbmsg:", dbmsg)
	logger.Println("\n>>>Return to App Client:", dbmsg.Data)
	fmt.Fprint(response, dbmsg.Data)
}
